<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>任务管理</el-breadcrumb-item>
      <el-breadcrumb-item>任务列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <!-- 搜索 -->
      <el-input
        placeholder="请输入内容"
        class="input-with-select"
        v-model="queryInfo.query"
        clearable
        style="width: 30%; margin-bottom: 15px"
        @clear="getTaskList"
        @keyup.enter.native="getTaskList"
      >
        <el-button
          slot="append"
          type="primary"
          icon="el-icon-search"
          @click="getTaskList"
        ></el-button>
      </el-input>

      <el-table
        :data="taskList"
        border
        highlight-current-row
        v-loading="loadingSwitch"
        style="width: 100%"
        row-key="_id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="taskname" label="任务名称"> </el-table-column>
        <el-table-column prop="" label="创建时间" width="120px" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.createdAt | date }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="结束时间" width="120px" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.endtime | date }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="creator.name"
          label="创建人"
          width="100px"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="" label="负责人">
          <template slot-scope="scope">
            <span
              class="worker_item"
              v-for="(item, index) in scope.row.worker"
              :key="index"
            >
              {{ item.name }}
            </span>
          </template>
        </el-table-column>

        <el-table-column
          prop="status"
          label="任务状态"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <div
              v-if="scope.row.status == 0"
              style="color: #67c23a; font-weight: bold"
            >
              进行中
            </div>
            <div
              v-if="scope.row.status == 1"
              style="color: #e6a23c; font-weight: bold"
            >
              审核中
            </div>
            <div
              v-if="scope.row.status == 2"
              style="color: #f56c6c; font-weight: bold"
            >
              被打回
            </div>
            <div
              v-if="scope.row.status == 3"
              style="color: #67c23a; font-weight: bold"
            >
              已完成
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="345px">
          <template v-slot="scope">
            <el-button
              type="success"
              size="mini"
              @click="$router.push(`/auditConfirm/${scope.row._id}`)"
              >查看</el-button
            >
            <el-button
              type="primary"
              size="mini"
              @click="$router.push(`/taskAdd/${scope.row._id}`)"
              >添加</el-button
            >
            <el-button
              type="info"
              size="mini"
              @click="$router.push(`/createTask/${scope.row._id}`)"
              >编辑</el-button
            >
            <!-- 审核按钮 -->
            <!-- <el-button v-if="scope.row.status == '0'" type="success" size="mini" @click="examine(scope.row)"
              >提交</el-button
            >
            <el-button v-if="scope.row.status == '1'" type="danger" size="mini" @click="examine(scope.row)"
              >打回</el-button
            >
            <el-button v-if="scope.row.status == '2'" type="success" size="mini" @click="examine(scope.row)"
              >提交</el-button
            > -->

            <el-button type="danger" size="mini" @click="taskDel(scope.row)"
              >删除</el-button
            >
            <el-button
              type="warning"
              size="mini"
              @click="exportA(scope.row._id)"
            >
              <!-- <a :href="`http://localhost:3000/admin/api/export/download/${scope.row._id}`" class="exportA">导出</a> -->
              导出
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        style="margin-top: 20px"
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import dayjs from "dayjs";
export default {
  filters: {
    date(val) {
      return dayjs(val).format("YYYY-MM-DD");
    },
  },
  data() {
    return {
      taskList: [],
      queryInfo: {
        query: "",
        pagenum: 1, //第几页
        pagesize: 7, //每页显示个数
      },
      //总条数
      total: 0,
      //加载开关
      loadingSwitch: true,
    };
  },
  methods: {
    //获取任务列表
    async getTaskList() {
      const res = await this.$http.get("/task", { params: this.queryInfo });
      let data = res.data;
      if (data.status === 200) {
        let time = setTimeout(() => {
          this.taskList = data.data.model;
          this.queryInfo.pagenum = data.data.pagenum;
          this.total = data.data.total;
          this.loadingSwitch = false;
        }, 200);
      }
    },

    //提交审核
    examine(row) {
      this.$confirm(`是否确定提交 ${row.taskname}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let data = {
            taskId: row._id,
            reviewer: row.creator,
            grade: row.grade,
          };
          let res = await this.$http.post("/taskaudit", data);
          this.$message.success("提交审核成功~~");
          this.getTaskList();
        })
        .catch((err) => {
          if (err === "cancel") {
            this.$message({
              type: "info",
              message: "已取消提交",
            });
          }
        });
    },

    //删除任务
    taskDel(row) {
      this.$confirm(`是否确定删除 ${row.taskname} 任务`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.delete(`/task/${row._id}`);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getTaskList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //页数改变触发事件
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getTaskList();
    },

    // 导出文件
    exportA(id) {

      this.$confirm(`确认要导出文件吗, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "导出成功!",
          });
          window.open(
            `https://quanquan.asia/admin/api/export/downloadChildren/${id}`,
            "_self"
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
  },
  created() {
    this.getTaskList();
  },
};
</script>

<style lang="less" scoped>
.exportA {
  color: #fff;
  text-decoration: none;
}
</style>